<?php
    function debug($var) {
        echo '<pre style="text-align: left;font-size: 14;">';
        $trace = debug_backtrace();
        echo 'Line: ' . $trace[0]['line'] . '<br>';
        print_r($var);
        echo '</pre>';
    }
    
    function debugdie($var) {
        echo '<pre style="text-align: left;font-size: 14;">';
        $trace = debug_backtrace();
        echo 'Line: ' . $trace[0]['line'] . '<br>';
        print_r($var);
        echo '</pre>';
        die();
    }
    
    function readCSV($csvFile) {
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle)) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        }
        fclose($file_handle);
        return $line_of_text;
    }
    
    function csv2arr($filePath, $arrKeyMap = array()) {
        if (($handle = fopen($filePath, "r")) === FALSE || !is_file($filePath)) return false;
        $i = 0;
        $result = array();
        $arrkeyNew = array();
        while (($rows = fgetcsv($handle)) !== FALSE) {
            if ($i == 0) {
                if (!empty($arrKeyMap)) {
                $arrkeyNew = mapKeyCSV($rows, $arrKeyMap);
            } else {
                $arrkeyNew = $rows;
            }
        } else {
            if (count($arrkeyNew) > count($rows)) {
                $count = count($arrkeyNew) - count($rows);
                for($i = 0; $i < $count; $i++) {
                    $rows[] = '';
                }
            }
            $result[] = array_combine($arrkeyNew, $rows);
            /*
             * debug($arrkeyNew); debug($rows); debug($result);die;
             */
        }
        $i++;
    }
    fclose($handle);
    if ($result) return $result;
    else
        return false;
}
function mapKeyCSV($header, $arraySrc) {
    $headerName = array();
    foreach ((array) $header as $title) {
            //$title = mb_convert_encoding($title, "UTF-8", "SJIS");
            $headerName[] = $arraySrc[trim($title)];
        }
        return $headerName;
    }
    
    function exportHeaderCsv($header = array()){
        ob_clean();
        header('Content-Disposition: attachment; filename="template_csv_import.csv"');
        header('Content-type: text/csv');
        if($header)
            return _arrayToCSV($header);
        else
            return false;
    }
    
    function _arrayToCSV($data) {
        $outstream = fopen("php://temp", 'w');
        fputcsv($outstream, $data, ',', '"');
        rewind($outstream);
        $csv = preg_replace('/\r\n|\n/', "\r\n", stream_get_contents($outstream));
        fclose($outstream);
        return mb_convert_encoding($csv, 'Shift_JIS', 'UTF-8, eucJP-win, ISO-2022-JP, UTF-7, ASCII, EUC-JP, '.mb_internal_encoding() . ', SJIS, Shift_JIS, SJIS-win, JIS');
    }
    
    /**
     * Generatting CSV formatted string from an array.
     * By Sergey Gurevich.
     */
    function array_to_csv($array, $header_row = true, $col_sep = ",", $row_sep = "\n", $qut = '"') {
        if (!is_array($array) or !is_array($array[0])) return false;
    
        $output = '';
        
        //Header row.
        if ($header_row) {
            foreach ($array[0] as $key => $val) {
                //Escaping quotes.
                $key = str_replace($qut, "$qut$qut", $key);
                $output .= "$col_sep$qut$key$qut";
            }
            $output = substr($output, 1)."\n";
        }
        //Data rows.
        foreach ($array as $key => $val) {
            $tmp = '';
            foreach ($val as $cell_key => $cell_val) {
                //Escaping quotes.
                $cell_val = str_replace($qut, "$qut$qut", $cell_val);
                $tmp .= "$col_sep$qut$cell_val$qut";
            }
            $output .= substr($tmp, 1).$row_sep;
        }
    
        return $output;
    }